//
//  TextUtils.m
//  Driver
//
//  Created by sfxd on 16/3/23.
//  Copyright © 2016年 速发讯达科技（北京）有限公司. All rights reserved.
//

#import "TextUtils.h"

@implementation TextUtils

// 字符串是否为空
+ (BOOL)isEmpty:(NSString*)string{
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if (string.length == 0) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

+ (NSString *)formatDecimalNumber:(CGFloat)num {
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setMaximumFractionDigits:4]; // 设置保留的小数位数
    
    NSNumber *number = [NSNumber numberWithFloat:num];
    
    NSString *formattedString = [numberFormatter stringFromNumber:number];
    return formattedString;
}

// 将金额从 "分" 转换成 "元"，并保留两位小数
+ (NSString *)convertAmountFromFenToYuan:(NSNumber *)amountNumber {
    
    if (amountNumber.integerValue < 0) {
        return [NSString stringWithFormat:@"%.2f", amountNumber.integerValue / 100.0];
    }
    
    NSDecimalNumber *amountDecimal = [NSDecimalNumber decimalNumberWithMantissa:amountNumber.unsignedLongLongValue exponent:-2 isNegative:NO];
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    formatter.minimumFractionDigits = 2;
    formatter.maximumFractionDigits = 2;
    formatter.usesGroupingSeparator = NO;
    
    return [formatter stringFromNumber:amountDecimal];
}

@end
